<div id="pkg_list" <{if empty($conf.objs)}> style="display:none" <{/if}> >
  <input type="hidden" name="objtype[]" value="pkg"/>
  <h5 style="text-align:center;">捆绑商品</h5>
  <table class="pkg gridlist" width="100%" cellspacing="0" cellpadding="0" border="0" >
      <thead>
        <tr>
            <th>货号</th>
            <th>商品名称</th>
            <th>规格</th>
            <th>购买数量</th>
            <th>商品价格</th>
            <th>操作</th>
        </tr>
     </thead>
     <tbody>
        <{foreach from=$conf.objs item=obj}>
        <{foreach from=$obj.order_items item=item}>
        <{if $item.delete == 'true'}>
        <{assign var='pkg_del' value="true"}>
        <{else}>
        <{assign var='pkg_del' value="false"}>
        <{/if}>
        <{/foreach}>
        <tr class="bindtitle" pkg-type="pkg" g-id="<{$obj.goods_id}>" pkg-id="<{$obj.goods_id}>" <{if $pkg_del=='true'}>style="background-color: #D8D8D8;" pkg-del='b'<{else}> pkg-del="a"<{/if}>>
          <td class="goods_bn"></td>
          <td class="goods_name"><{$obj.name}></td>
          <td class="goods_addon">捆绑商品</td>
          <td class="goods_num"><input type="text" id="pkgnum_<{$obj.goods_id}>" pkg-id="<{$obj.goods_id}>" aid="<{$obj.goods_id}>" vtype="required&&unsignedint" atype="num_obj" value="<{$obj.quantity}>" name="pkg[num][<{$obj.obj_id}>]" size="4" <{if $pkg_del == 'true'}>disabled="true"<{/if}>  onchange="total();" /></td>
          <td class="goods_price"><input type="text" id="pkgpr_<{$obj.goods_id}>" aid="<{$obj.goods_id}>" value="<{$obj.price}>" name="pkg[price][<{$obj.obj_id}>]" size="8" <{if $pkg_del == 'true'}>disabled="true"<{/if}> onchange="total();" /></td>
          <td class="goods_op">
          <{if $pkg_del == 'true'}>
            <{button type="button" label="恢复" id="bt_{$obj.goods_id}" onclick="del_pkg('{$obj.goods_id}')" }>
          <{else}>
            <{button type="button" label="删除" id="bt_{$obj.goods_id}" onclick="del_pkg('{$obj.goods_id}')" }>
          <{/if}>
          </td>
        </tr>
        <{foreach from=$obj.order_items item=item}>
        <tr id="tr_<{$item.item_id}>" pkg-id="<{$obj.goods_id}>" g-pid="<{$item.product_id}>" g-type="<{$item.item_type}>" pkg-type="<{$item.item_type}>" g-shop-pid="<{$item.shop_product_id}>" aid="<{$item.item_id}>" <{if $item.delete == 'true'}>style="background-color: #D8D8D8;" pkg-del="b"<{else}> pkg-del="a"<{/if}>>
          <td class="item_bn"><{$item.bn}></td>
          <td class="item_name"><{$item.name}></td>
          <td class="item_addon"><{$item.addon}></td>
          <td class="item_num">
            <input type="hidden" value="<{$item.item_id}>" name="pkg[obj][<{$obj.obj_id}>][]" />
            <input type="text" class="none_input" id="num_<{$item.item_id}>" name="pkg[inum][<{$obj.obj_id}>]" pg-id="<{$obj.goods_id}>" aid="<{$item.item_id}>" size="4" readonly="true" js="<{$item.quantity/$obj.quantity}>" <{if $item.delete == 'true'}>disabled="true"<{/if}> value="<{$item.quantity/$obj.quantity}>" />
          </td>
          <td class="item_price">
          <input type="text" class="none_input" value="0" id="pr_<{$item.item_id}>" name="pkg[iprice][<{$obj.obj_id}>]" size="8" readonly="true" /></td>
          <td class="item_op"></td>
        </tr>
        <{/foreach}>
        <{/foreach}>
     </tbody>
</table>
</div>
<script>

  function total_pkg(){
    var total = 0;
    $('pkg_list').getElements("input[type=text]").each(function (e){
        if (!e.disabled){
            if (/^\d+(\.\d+)?$/.test(e.value)){
                if (e.get('atype') == 'num'){
                    if (parseInt(e.value) <= 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('pr_'+id).value).toFixed(3);
                }else if (e.get('atype') == 'newnum'){
                    if (parseInt(e.value) <= 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('newpr_'+id).value).toFixed(3);
                }else if (e.get('atype') == 'num_obj'){
                    if (parseInt(e.value) == 0){
                        var _ca = e.getNext();
                        if (!_ca) new Element('span',{'class':'error caution notice-inline',html:'请录入不小于0的数'}).injectAfter(e);
                    }else {
                        var _ca = e.getNext();
                        if (e.value > 0){
                            if (_ca) _ca.remove();
                        }
                    }
                    if (e.get('pkg-id') != null || e.get('pkg-id') != ''){
                        $('pkg_list').getElements('input[type=text]').each(function (eee){
                            if (eee.get('pg-id') == e.get('pkg-id')){
                                eee.value = eee.get('js')*e.value;
                            }
                        });
                    }
                    id = e.get('aid');
                    total += parseFloat(e.value).toFixed(3)*parseFloat($('pkgpr_'+id).value).toFixed(3);
                }
            }
            else{
              var _ca = e.getNext();
              if (!_ca) new Element('span',{'class':"error caution notice-inline",html:"请录入不小于0的数"}).injectAfter(e);
            }
        }
      });
    return total;
  }
//删除捆绑商品
function del_pkg(id){
    $ES('#pkg_list tbody tr').each(function (e){
        if (e.get('pkg-id') == id){
            if (e.get('newpkg') == '1'){
                e.remove();
            }else {
                if (e.get('pkg-del') == 'b'){
                    e.getElements('input[type=text]').each(function (a){a.disabled=false;});
                    if ($("bt_"+id)) $("bt_"+id).getElement('span').getElement('span').set('text','删除');
                    e.set('pkg-del','a');
                    e.setStyle('background-color','');
                }else {
                    e.getElements('input[type=text]').each(function (a){a.disabled=true;});
                    if ($("bt_"+id)) $("bt_"+id).getElement('span').getElement('span').set('text','恢复');
                    e.set('pkg-del','b');
                    e.setStyle('background-color','#D8D8D8');
                }
            }
        }
    });
    total();
}
//删除捆绑商品
function del_pkg_new(id){
    $ES('#pkg_list tbody tr[pkg-type]').each(function (e){
        if (e.get('pkg-id') == id){
            if (e.get('newpkg') == '1'){
                e.remove();
            }else {
                if (e.get('pkg-del') == 'b'){
                    e.getElements('input[type=text]').each(function (a){a.disabled=false;});
                    if ($("bt_"+id)) $("bt_"+id).getElement('span').getElement('span').set('text','删除');
                    e.setProperty('pkg-del','a');
                    e.setStyle('background-color','');
                }else {
                    e.getElements('input[type=text]').each(function (a){a.disabled=true;});
                    if ($("bt_"+id)) $("bt_"+id).getElement('span').getElement('span').set('text','恢复');
                    e.setProperty('pkg-del','b');
                    e.setStyle('background-color','#D8D8D8');
                }
            }
        }
    });
    total();
    if (!$E('#pkg_list tbody').getElement('td')){
    	$('pkg_list').style.display = "none";
    }
}

$('add_pkg_product').addEvent('click',function(e){
    var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=omepkg&ctl=admin_pkg&act=findPkgProduct');
    Ex_Loader('modedialog',function() {
        new finderDialog(url,{params:{url:'index.php?app=omepkg&ctl=admin_pkg&act=getPkgProducts',name:'goods_id[]'},handle:this,width:1000,height:660,
            onCallback:function(rs){
                if(!rs)return;
                rs=JSON.decode(rs);
                if (rs!=''){
                    rs.each(function (ee){
                        createpkg(ee);
                    });
                }
            }
        });
    }.bind(this));
});
function createpkg(rs){
    var ishave='false';
    $ES('#pkg_list tbody tr').each(function (es){
        if (es.get('g-id') == rs.goods_id){
            if (es.get('pkg-del')==null || es.get('pkg-del')=='a'){
                ishave='true'
                alert("有此捆绑商品："+rs.name);
                $("pkgnum_"+rs.goods_id).focus();
            }else {
                ishave='true'
                alert("请恢复捆绑商品："+rs.name);
            }
        }
    });
    if (ishave!='true'){
        var ptr = new Element('tr.bindtitle').inject($E('#pkg_list tbody'));
        ptr.setProperty('g-id',rs.goods_id);
        ptr.setProperty('pkg-id',rs.goods_id);
        ptr.setProperty('pkg-type','pkg');
        ptr.setProperty('pkg-del','a');
        ptr.setProperty('newpkg','1');
        new Element('td', {html:'-'}).inject(ptr);
        new Element('td', {html:rs.name}).inject(ptr);
        new Element('td', {html:'捆绑商品'}).inject(ptr);
        var td1 = new Element('td').inject(ptr);
        var td2 = new Element('td').inject(ptr);
        var td3 = new Element('td').inject(ptr);
        var tmp = new Element('input[type=text]', {id:'pkgnum_'+rs.goods_id, vtype:'number&&unsignedint', name:'pkg[pkgonum]['+rs.goods_id+']', value:"1", size:'4',events:{'change':function(){total();}}}).inject(td1);
        tmp.setProperty('atype','num_obj');
        tmp.setProperty('pkg-id',rs.goods_id);
        tmp.setProperty('aid',rs.goods_id);
        new Element('input[type=text]', {id:'pkgpr_'+rs.goods_id, vtype:'number&&unsigned', name:'pkg[pkgopr]['+rs.goods_id+']', value:"0", size:'8',events:{'change':function(){total();}}}).inject(td2);
        new Element('button.btn', {id:'btn_'+rs.goods_id, type:'button', html:'<span><span>删除</span></span>',events:{'click':function(){del_pkg_new(rs.goods_id);}}}).inject(td3);
        rs['items'].each(function (e){
            createpkgproduct(rs,e);
        });
    }
    if ($('pkg_list').style.display=='none'){
        $('pkg_list').style.display = "";
    }
}

function createpkgproduct(rs,e){
    var trr = new Element('tr', {id:'tr_'+e.product_id}).inject($E('#pkg_list tbody'));
    trr.setProperty('g-pid',e.product_id);
    trr.setProperty('pkg-id',rs.goods_id);
    trr.setProperty('pkg-type','pkg');
    trr.setProperty('pkg-del','a');
    trr.setProperty('newpkg','1');
    new Element('td', {html:e.bn}).inject(trr);
    new Element('td', {html:e.name}).inject(trr);
    new Element('td', {html:'-'}).inject(trr);
    var td1 = new Element('td').inject(trr);
    var td2 = new Element('td').inject(trr);
    var td3 = new Element('td').inject(trr);
    var tmp_bn = new Element('input[type=hidden]', {name:'bn_list[]', value:e.bn}).inject(td1);
    var tmp = new Element('input.none_input[type=text]', {id:'pkgnum_'+e.product_id, vtype:'number&&unsignedint', readOnly:true,name:'pkg[pkgnum]['+rs.goods_id+']['+e.product_id+']', value:e.pkgnum, size:'5',events:{'change':function(){total();}}}).inject(td1);
    tmp.setProperty('atype','pkgnum');
    tmp.setProperty('aid',e.product_id);
    tmp.setProperty('pg-id',rs.goods_id);
    tmp.setProperty('js',e.pkgnum);
    new Element('input.none_input[type=text]', {id:'pkgpr_'+e.product_id, vtype:'number&&unsigned', name:'pkg[pkgpr]['+rs.goods_id+']['+e.product_id+']', value:"0", readonly:true, size:'8',events:{'change':function(){total();}}}).inject(td2);
}

</script>
